package com.demo.solution;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Scanner;

public class Solution {
    public static void main(String[] args) throws  Exception{

        Scanner sc = new Scanner(new FileInputStream("D:/tmp/xx.txt"));
        Double sum = 0.0;
        while (sc.hasNext()) {
            sum += sc.nextDouble();
        }
        sc.close();

        System.out.println("sum = " + sum);

    }

    static void test() {
        Scanner sc = new Scanner(System.in);
        String nums = sc.next();
        sc.close();

        List<Integer> ss = new ArrayList<Integer>();
        for (String num : nums.split(",")) {
            ss.add(Integer.parseInt(num));
        }

        Integer[] h = ss.toArray(new Integer[]{});

        int i = new Solution().maxArea(h);
        int j = new Solution().maxArea1(h);
        int m = new Solution().maxArea2(h);
        System.out.println("i = " + i);
        System.out.println("j = " + j);
        System.out.println("j = " + m);
    }

    public int maxArea(Integer[] height) {
        int i = 0, j = height.length - 1;
        int max = 0;
        while (i < j) {
            int h = min(height[i], height[j]);
            int area = h * (j - i);
            if (area > max) {
                max = area;
            }
            while (i < j && height[i] <= h) i++;
            while (i < j && height[j] <= h) j--;
        }
        return max;
    }

    public int maxArea2(Integer[] height) {

        int max = 0;
        for (int step = 1; step < height.length; step++) {
            int maxHigh = 0;
            for (int i = 0; i < height.length - step; i++) {
                int h = min(height[i], height[i + step]);
                if (h > maxHigh) {
                    maxHigh = h;
                }
            }
            int area = maxHigh * step;
            if (area > max) {
                max = area;
            }
        }
        return max;
    }


    public int maxArea1(Integer[] height) {

        int max = 0;
        int maxHeigh = -1;

        for (int i = 0; i < height.length; i++) {
            if (height[i] > maxHeigh) {
                maxHeigh = height[i];
                for (int j = i + 1; j < height.length; j++) {

                    int area = min(height[i], height[j]) * (j - i);
                    if (area > max) {
                        max = area;
                    }

                }
            }
        }
        return max;
    }

    private static int min(int a, int b) {
        return a < b ? a : b;
    }
}